GBML manual / Maths

sign_int

The same as sign, for a whole number: -1 when value is negative, 1 when it is positive, 0 when it is exactly 0 - and the answer is a whole number, so it can go on to anything that wants one, such as the frame of draw_sprite.

Syntax

sign_int(value) -> int

Arguments

NameType
valueint

Returns

int

Description

The same as sign, for a whole number: -1 when value is negative, 1 when it is positive, 0 when it is exactly 0 - and the answer is a whole number (so is sign's, given whole numbers). sign_int answers "which way," with no opinion at all about "how much."

Reach for this one when the answer is going to be handed on to something that wants a whole number, such as the frame of draw_sprite. Use sign when the answer goes into measured maths, such as a speed.

Example

var way = sign_int(int_floor(x) - int_floor(xstart));

Works out which side of its starting point this object is on: -1 when it is to the left, 1 when it is to the right, 0 when both are on the same whole pixel. way is a whole number, so it can be handed to anything that wants one.

Why there are two of these

sign and sign_int do the same arithmetic, and given a whole number sign already answers a whole number - so facing = sign(int_floor(x) - 120); keeps facing a whole number. The only difference left is strictness: sign_int refuses a decimal.

The number going in has to be whole

sign_int takes a whole number only. A number with a point in it is a real - even 4.0 - and is refused when the project is checked. x is a real, so this is refused:

facing = sign_int(x - 120);
Argument 1 to `sign_int` should be an int, but this is a real.

If you have a real and want a whole number out of it, round it first with int_floor, int_ceil or int_round, as the example above does. If you want the sign of a real and are happy with a real back, use sign.

Cost

Two comparisons - as cheap as this group gets, safe to call every frame.

On the GBA

sign and sign_int do the same arithmetic. Given a whole number, sign already hands back a whole number, so sign_int is now just a stricter spelling: it refuses a decimal outright. The number going in has to be whole too: sign_int(x - 120) is refused when the project is checked because x is a real - round it first with int_floor, int_ceil or int_round. Two comparisons, cheap enough for every frame.

See also: signabsmin_intmax_intint_floor